/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.game.entitysystem.systems;

import com.jme3.bullet.control.CharacterControl;
import com.jme3.scene.Node;
import etherblocks.engine.objects.ModelObject;
import etherblocks.game.entitysystem.*;
import etherblocks.game.entitysystem.components.*;

/**
 *
 * @author Carl
 */
public abstract class PhysicsTransformSystem extends ModelObjectSystem{

    public PhysicsTransformSystem(Node rootNode, int playerEntityID){
        super(rootNode);
        this.playerEntityID = playerEntityID;
    }
    private int playerEntityID;
    
    @Override
    public void onNextFrameCalculation(EntityMap entityMap, float lastTimePerFrame){
        for(Entity entity : entityMap.getEntities(PhysicsComponent.class)){
            if(entity.getID() != playerEntityID){
                ModelObject modelObject = getModelObject(entity);
                if(modelObject != null){
                    CharacterControl characterControl = modelObject.getControl(CharacterControl.class);
                    if(characterControl != null){
                        if(isApplyingTransformAllowed_Location(entity)){
                            LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
                            characterControl.setPhysicsLocation(locationComponent.getLocation());
                            onTransformApplied_Location(entity);
                        }
                        if(isApplyingTransformAllowed_Rotation(entity)){
                            RotationComponent rotationComponent = entity.getComponent(RotationComponent.class);
                            characterControl.setViewDirection(rotationComponent.getViewDirection());
                            onTransformApplied_Rotation(entity);
                        }
                    }
                }
            }
        }
    }
    
    protected abstract boolean isApplyingTransformAllowed_Location(Entity entity);
    
    protected abstract boolean isApplyingTransformAllowed_Rotation(Entity entity);
    
    protected void onTransformApplied_Location(Entity entity){
        
    }
    
    protected void onTransformApplied_Rotation(Entity entity){
        
    }
}
